package com.neusoft.admin.controller;

import com.neusoft.grid.entity.AQI;
import com.neusoft.admin.entity.TableData;
import javafx.application.Platform;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ScrollBar;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;

public class AQItrendtotalstatisViewControl {
    @FXML
    private TableView<TableData> tableView;
    private static final String AQI_FILE = "src/com/neusoft/grid/data/AQI.txt";
    private List<AQI> aqilist = new ArrayList<>();

    private int count1 = 0;
    private int count2 = 0;
    private int count3 = 0;
    private int count4 = 0;
    private int count5 = 0;
    private int count6 = 0;
    private int count7 = 0;
    private int count8 = 0;
    private int count9 = 0;
    private int count10 = 0;
    private int count11 = 0;
    private int count12 = 0;

    // 表格的固定高度（根据实际需求调整）
    private static final double TABLE_HEIGHT = 533;
    // 表格头部高度估计值
    private static final double TABLE_HEADER_HEIGHT = 30;
    // 表格底部空白估计值
    private static final double TABLE_PADDING = 10;

    private List<AQI> loadAQIData() throws IOException {
        File file = new File(AQI_FILE);
        if (!file.exists() || file.length() == 0) {
            return new ArrayList<>();
        }

        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
            return (List<AQI>) ois.readObject();
        } catch (ClassNotFoundException e) {
            throw new IOException("数据文件格式错误", e);
        }
    }

    @FXML
    private void initialize() {
        try {
            aqilist = loadAQIData();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        for (AQI month : aqilist) {
            if (month.getMonth() == 6 && month.getAQI() >= 101) {
                count1++;
            } else if (month.getMonth() == 5 && month.getAQI() >= 101) {
                count2++;
            } else if (month.getMonth() == 4 && month.getAQI() >= 101) {
                count3++;
            } else if (month.getMonth() == 3 && month.getAQI() >= 101) {
                count4++;
            } else if (month.getMonth() == 2 && month.getAQI() >= 101) {
                count5++;
            } else if (month.getMonth() == 1 && month.getAQI() >= 101) {
                count6++;
            } else if (month.getMonth() == 12 && month.getAQI() >= 101) {
                count7++;
            } else if (month.getMonth() == 11 && month.getAQI() >= 101) {
                count8++;
            } else if (month.getMonth() == 10 && month.getAQI() >= 101) {
                count9++;
            } else if (month.getMonth() == 9 && month.getAQI() >= 101) {
                count10++;
            } else if (month.getMonth() == 8 && month.getAQI() >= 101) {
                count11++;
            } else if (month.getMonth() == 7 && month.getAQI() >= 101) {
                count12++;
            }
        }

        ObservableList<TableData> data = FXCollections.observableArrayList(
                new TableData("1", "2025-06", String.valueOf(count1)),
                new TableData("2", "2025-05", String.valueOf(count2)),
                new TableData("3", "2025-04", String.valueOf(count3)),
                new TableData("4", "2025-03", String.valueOf(count4)),
                new TableData("5", "2025-02", String.valueOf(count5)),
                new TableData("6", "2024-01", String.valueOf(count6)),
                new TableData("7", "2024-12", String.valueOf(count7)),
                new TableData("8", "2024-11", String.valueOf(count8)),
                new TableData("9", "2024-10", String.valueOf(count9)),
                new TableData("10", "2024-09", String.valueOf(count10)),
                new TableData("11", "2024-08", String.valueOf(count11)),
                new TableData("12", "2024-07", String.valueOf(count12))
        );
        tableView.setItems(data);
        // 设置表格固定高度
        tableView.setPrefHeight(TABLE_HEIGHT);
        tableView.setMaxHeight(TABLE_HEIGHT);
        tableView.setMinHeight(TABLE_HEIGHT);

        // 计算并设置行高
        double rowHeight = (TABLE_HEIGHT - TABLE_HEADER_HEIGHT - TABLE_PADDING) / data.size();
        tableView.setFixedCellSize(rowHeight);

        // 隐藏垂直滚动条
        setupCellFormatting();
        hideVerticalScrollBar();
    }

    private void hideVerticalScrollBar() {
        // 使用Platform.runLater确保在UI渲染完成后执行
        Platform.runLater(() -> {
            for (Node node : tableView.lookupAll(".scroll-bar")) {
                if (node instanceof ScrollBar) {
                    ScrollBar scrollBar = (ScrollBar) node;
                    if (scrollBar.getOrientation().equals(javafx.geometry.Orientation.VERTICAL)) {
                        scrollBar.setMaxWidth(0);
                        scrollBar.setVisible(false);
                        scrollBar.setManaged(false);
                    }
                }
            }
        });
    }

    private void setupCellFormatting() {
        for (TableColumn column : tableView.getColumns()) { // 移除泛型参数
            column.setCellFactory(tc -> {
                TableCell cell = new TableCell<TableData, Object>() {
                    @Override
                    protected void updateItem(Object item, boolean empty) {
                        super.updateItem(item, empty);
                        if (empty) {
                            setText(null);
                        } else {
                            setText(item.toString());
                            setAlignment(javafx.geometry.Pos.CENTER);
                            setStyle("-fx-font-size: 18px;");
                        }
                    }
                };
                return cell;
            });
        }
    }


}
